/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aionengine.gameserver.world.zone.handler;

import com.aionengine.gameserver.controllers.observer.ActionObserver;
import com.aionengine.gameserver.controllers.observer.CollisionMaterialActor;
import com.aionengine.gameserver.controllers.observer.IActor;
import com.aionengine.gameserver.geoEngine.scene.Spatial;
import com.aionengine.gameserver.model.Race;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.templates.materials.MaterialSkill;
import com.aionengine.gameserver.model.templates.materials.MaterialTemplate;
import com.aionengine.gameserver.world.zone.ZoneInstance;
import javolution.util.FastMap;

/**
 * @author Rolandas
 */
public class MaterialZoneHandler implements ZoneHandler {

    FastMap<Integer, IActor> observed = new FastMap<Integer, IActor>();

    private Spatial geometry;
    private MaterialTemplate template;
    private boolean actOnEnter = false;
    private Race ownerRace = Race.NONE;

    public MaterialZoneHandler(Spatial geometry, MaterialTemplate template) {
        this.geometry = geometry;
        this.template = template;
        String name = geometry.getName();
        if (name.indexOf("FIRE_BOX") != -1 || name.indexOf("FIRE_SEMISPHERE") != -1 || name.indexOf("FIREPOT") != -1 ||
                name.indexOf("FIRE_CYLINDER") != -1 || name.indexOf("FIRE_CONE") != -1 || name.startsWith("BU_H_CENTERHALL"))
            actOnEnter = true;
        if (name.startsWith("BU_AB_DARKSP"))
            ownerRace = Race.ASMODIANS;
        else if (name.startsWith("BU_AB_LIGHTSP"))
            ownerRace = Race.ELYOS;
    }

    @Override
    public void onEnterZone(Creature creature, ZoneInstance zone) {
        if (ownerRace == creature.getRace())
            return;
        MaterialSkill foundSkill = null;
        for (MaterialSkill skill : template.getSkills()) {
            if (skill.getTarget().isTarget(creature)) {
                foundSkill = skill;
                break;
            }
        }
        if (foundSkill == null)
            return;
        CollisionMaterialActor actor = new CollisionMaterialActor(creature, geometry, template);
        creature.getObserveController().addObserver(actor);
        observed.put(creature.getObjectId(), actor);
        if (actOnEnter)
            actor.act();
    }

    @Override
    public void onLeaveZone(Creature creature, ZoneInstance zone) {
        IActor actor = observed.get(creature.getObjectId());
        if (actor != null) {
            creature.getObserveController().removeObserver((ActionObserver) actor);
            observed.remove(creature.getObjectId());
            actor.abort();
        }
    }

}
